package com.hcs.chenyu.moreaboutstudent;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.hcs.chenyu.R;

public class deletestudent extends AppCompatActivity implements View.OnClickListener {
    private TextView mstuNoText;
    private TextView mstuNameText;
    private TextView mstusexText;
    private TextView mdepartmentText;
    private TextView mclassesText;
    private TextView mtelText;
    private TextView mdormNoText;
    private Button msearchstudentid;
    private Button mdeleteSTudent;
    private Button mbtn_ToMain21;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deletestudent);
        initview();

    }

    private void initview() {
        mstuNoText = findViewById(R.id.stuNoText);
        mstuNameText = findViewById(R.id.stuNameText);
        mstusexText = findViewById(R.id.stusexText);
        mdepartmentText = findViewById(R.id.departmentText);
        mclassesText = findViewById(R.id.classesText);
        mtelText = findViewById(R.id.telText);
        mdormNoText = findViewById(R.id.dormNoText);
        msearchstudentid = findViewById(R.id.searchstudentid);
        mdeleteSTudent = findViewById(R.id.deleteSTudent);
        mbtn_ToMain21 = findViewById(R.id.btn_ToMain3);
        msearchstudentid.setOnClickListener((View.OnClickListener) this);
        mdeleteSTudent.setOnClickListener((View.OnClickListener) this);
        mbtn_ToMain21.setOnClickListener((View.OnClickListener) this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.searchstudentid:   //查询学生id操作
                searchstudentid();
                break;
            case R.id.deleteSTudent:    //删除操作
                deleteSTudent();
                break;
            case R.id.btn_ToMain3:
                Intent intent = new Intent(deletestudent.this, more.class);
                startActivity(intent);
        }
    }



    //查找学生个人信息
    private void searchstudentid() {
        String mstudentid=mstuNoText.getText().toString().trim();  //获取用户输入
        //建立数据库访问对象
        Studentdao da=new Studentdao(getApplicationContext());
        //打开数据库
        da.open();
        //调用数据库访问方法
        Student student = da.getstudents(mstudentid);
        //将数据填入控件
        mstuNameText.setText(student.mstudentname);
       mstusexText.setText(student.mstudentsex);
       mdepartmentText.setText(student.mstudentmajoy);
       mclassesText.setText(student.mstudentclass);
       mtelText.setText(student.mstudenttel);
       mdormNoText.setText(student.mstudentdormNo);
        //关闭数据库
        da.close();
    }
    //删除学生信息
    private void deleteSTudent() {
        Student student = new Student();
        student.mstudentid=mstuNameText.getText().toString().trim();
        //创建数据库访问对象
        Studentdao da=new Studentdao(getApplicationContext());
        //打开数据库
        da.open();
        //执行数据库访问方法
        int result= da.deletestudent(student);
        if(result>0) {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        }
        //关闭数据库
        da.close();
    }

}